package com.nq.service.impl;

/**

 * 开发者----股牛网络

 * 联系TG：gncms

 */


import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageInfo;

import com.nq.common.ServerResponse;

import com.nq.dao.AgentUserMapper;

import com.nq.dao.UserCashDetailMapper;

import com.nq.pojo.AgentUser;

import com.nq.pojo.User;

import com.nq.pojo.UserCashDetail;

import com.nq.pojo.vo.UserCashDetailVo;
import com.nq.service.IAgentUserService;

import com.nq.service.IUserCashDetailService;

import com.nq.service.IUserService;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.nq.utils.DateTimeUtil;
import com.nq.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

 @Service("iUserCashDetailService")
 public class UserCashDetailServiceImpl implements IUserCashDetailService {

   @Autowired
   UserCashDetailMapper userCashDetailMapper;

   @Autowired
   IUserService iUserService;

   @Autowired
   IAgentUserService iAgentUserService;

   @Autowired
   AgentUserMapper agentUserMapper;
   @Value("${spring.profiles.active}")
   private String active;



   public ServerResponse<PageInfo> findUserCashDetailList(Integer positionId, HttpServletRequest request, int pageNum, int pageSize) {

     PageHelper.startPage(pageNum, pageSize);



     User user = this.iUserService.getCurrentRefreshUser(request);
     if (user == null ){
       return ServerResponse.createBySuccessMsg("Please log in first.");
     }


     List<UserCashDetail> userCashDetails = this.userCashDetailMapper.findUserCashDetailList(user.getId(), positionId);



     PageInfo pageInfo = new PageInfo(userCashDetails);



     return ServerResponse.createBySuccess(pageInfo);

   }

   public ServerResponse<PageInfo> listByAgent(Integer userId, String userName, Integer agentId, Integer positionId, HttpServletRequest request, int pageNum, int pageSize) {

     AgentUser currentAgent = this.iAgentUserService.getCurrentAgent(request);
     if (currentAgent ==null){
       return    ServerResponse.createByError("Please log in first.",null);
     }


     if (agentId != null) {

       AgentUser agentUser = this.agentUserMapper.selectByPrimaryKey(agentId);

       if (agentUser.getParentId() != currentAgent.getId()) {

         return ServerResponse.createByErrorMsg("You can't query the positions of non-lower-level agent users.");

       }

     }

     Integer searchId = null;

     if (agentId == null) {

       searchId = currentAgent.getId();

     } else {

       searchId = agentId;

     }

     PageHelper.startPage(pageNum, pageSize);

     List<UserCashDetail> cashDetails = this.userCashDetailMapper.listByAgent(userId, userName, searchId, positionId);

     PageInfo pageInfo = new PageInfo(cashDetails);

     return ServerResponse.createBySuccess(pageInfo);

   }

   public ServerResponse<PageInfo> listByAdmin(Integer userId, String userPhone, String userName, String deType, Integer agentId, Integer positionId, int pageNum, int pageSize) {

     PageHelper.startPage(pageNum, pageSize);
     List<UserCashDetailVo> cashDetails = this.userCashDetailMapper.listByAdmin(userId, userPhone, userName, agentId, positionId, deType);
     PageInfo pageInfo = new PageInfo(cashDetails);
     return ServerResponse.createBySuccess(pageInfo);

   }


   public int deleteByUserId(Integer userId) { return this.userCashDetailMapper.deleteByUserId(userId); }

   public ServerResponse delCash(Integer cashId) {
     if (cashId == null) {
       return ServerResponse.createByErrorMsg("Delete-id cannot be empty");
     }
     int updateCount = this.userCashDetailMapper.deleteByPrimaryKey(cashId);
     if (updateCount > 0) {
       return ServerResponse.createBySuccessMsg("Deleted successfully");
     }
     return ServerResponse.createByErrorMsg("Failed to delete");
   }

   @Override
   public ServerResponse edit(Integer id, String deAmt, String addTime) {

     UserCashDetail userCashDetail = userCashDetailMapper.selectByPrimaryKey(id);
     if (null == userCashDetail) {
       return ServerResponse.createByErrorMsg("Details do not exist");
     }

     UserCashDetail cashDetail = new UserCashDetail();
     cashDetail.setId(id);
     cashDetail.setDeAmt(new BigDecimal(deAmt));

     Date date = DateTimeUtil.strToDate(addTime);
     cashDetail.setAddTime(date);
     userCashDetailMapper.updateByPrimaryKeySelective(cashDetail);
     return ServerResponse.createBySuccess("编辑成功");
   }

 }
